package com.jm.bishe.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.google.code.kaptcha.Constants;
import com.jm.bishe.entity.*;
import com.jm.bishe.service.Impl.AdminServiceImpl;
import com.jm.bishe.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/sys/admin")
public class AdminController {

    @Autowired
    private AdminServiceImpl adminServiceImpl;

    @PostMapping("/login")
    @ResponseBody
    public Result adminLogin(@RequestBody LoginBody loginBody, HttpServletRequest request){
        String checkCode = (String)request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        if (loginBody.getCode() != null && !loginBody.getCode().equals("")){
            if (checkCode.equals(loginBody.getCode())){
                return adminServiceImpl.login(loginBody.getPhoneNumber(),loginBody.getPassWord());
            }
            return Result.falure(2002,"验证码错误",null);
        }
        return Result.falure(2002,"请输入验证码",null);
    }


    @PostMapping("/addUser")
    @ResponseBody
    public Result adminAddUser(@RequestBody UserRegister userRegister,@CookieValue("token") String token){
        return adminServiceImpl.addUser(userRegister,JwtTokenUtil.getId(token));
    }

    @GetMapping("/queryUser")
    @ResponseBody
    public Result adminQueryUser(@RequestParam("phoneNumber")String phoneNumber,@CookieValue("token") String token){
        return adminServiceImpl.queryUserByPhoneNumber(phoneNumber,JwtTokenUtil.getId(token));
    }

    @GetMapping("/queryVip")
    @ResponseBody
    public Result adminQueryVip(@RequestParam("isVip")int isVip,@CookieValue("token")String token){
        return adminServiceImpl.queryUserByVip(isVip,JwtTokenUtil.getId(token));
    }

    @PostMapping("/updateUser")
    @ResponseBody
    public Result adminUpdateUser(@RequestBody UserUpdate userUpdate,@CookieValue("token")String token){
        return adminServiceImpl.updateUser(userUpdate,JwtTokenUtil.getId(token));
    }

    @GetMapping("/queryAllUser")
    @ResponseBody
    public Result adminQueryAllUser(@CookieValue("token")String token){
        return adminServiceImpl.queryAllUser(JwtTokenUtil.getId(token));
    }



}
